import csv
import random
from my_package import my_tools
from faker import Faker

list1 = []


def random_info(n=100):
    subjects = ['Python', 'java', 'C++', 'html']
    names = []
    for i in range(n // len(subjects)):
        fake_cn = Faker('zh_CN')
        name = fake_cn.name()
        names.append(name)
    for k in range(n):
        subject = random.choice(subjects)
        score = random.randint(50, 100)
        name = random.choice(names)
        for j in list1:
            if j[0] == name and j[1] == subject:
                break
        else:
            list1.append([name, subject, score])


def make_datas():
    with open('test1.csv', mode='a', encoding='utf-8', newline='') as f:
        cf = csv.writer(f)
        random_info()
        cf.writerows(list1)


def average():
    with open('test1.csv', mode='r', encoding='utf-8') as f:
        cf = csv.reader(f)
        head = next(cf)
        scores = []
        for line in cf:
            scores.append(int(line[2]))
        averager = sum(scores) / len(scores)
        return averager


make_datas()
average1 = average()
print("平均成绩为：", round(average1, 2))
